package dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.TTCCLayout;

import dao.ClientUsersDao;
import pojo.ClientUsers;
import util.DBUtils;

public class ClientUserDaoImpl implements ClientUsersDao {

	@Override
	public int add(ClientUsers t) {
		String sql = "insert into clientusers(id,uname,psd,image) values(?,?,?,?)";
		PreparedStatement pst = null;
		int m = 0;
		try {
			pst = DBUtils.createConnection().prepareStatement(sql);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, t.getUname());
			pst.setString(3, t.getPsd());
			pst.setString(4, t.getImage());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			m = 0;
		}
		return m;
	}

	@Override
	public ClientUsers findByName(String uname, String psd) {
		boolean loginResult = false;
		String sql = "select * from clientusers where uname = ?";
		PreparedStatement pst = null;
		ResultSet resultSet = null;
		try {
			pst = DBUtils.createConnection().prepareStatement(sql);
			pst.setString(1, uname);
			resultSet = pst.executeQuery();
			while (resultSet.next()) {
				if(resultSet.getString("psd").equals(psd)){
					ClientUsers resultUser = new ClientUsers();
					resultUser.setId(resultSet.getString("id"));
					resultUser.setUname(resultSet.getString("uname"));
					resultUser.setPsd(psd);
					resultUser.setImage(resultSet.getString("image"));
					return resultUser; 
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int delete(String id) {
		return 0;
	}

	@Override
	public int update(ClientUsers t) {
		return 0;
	}

	@Override
	public ClientUsers load(String id) {
		return null;
	}

	@Override
	public List<ClientUsers> findAll() {
		return null;
	}
}
